package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfDeviceInfo;
import com.yuandian.api.management.vo.DeviceInfoVo;
import com.yuandian.api.management.vo.MenuInfo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.mapper.ConfDeviceInfoMapper;
import com.yuandian.management.service.ConfDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author EDY
* @description 针对表【conf_device_info(设备名称字典表)】的数据库操作Service实现
* @createDate 2023-06-09 17:18:19
*/
@Service
public class ConfDeviceInfoServiceImpl extends ServiceImpl<ConfDeviceInfoMapper, ConfDeviceInfo>
    implements ConfDeviceInfoService{

	@Autowired
	ConfDeviceInfoMapper confDeviceInfoMapper;


	/**
	 * 获取基础设施的信息(设备)
	 *
	 * @return
	 */
	@Override
	public R getDeviceInfo() {
		List<ConfDeviceInfo> deviceInfos = list();
		List<MenuInfo> list = new ArrayList<>();
		for (ConfDeviceInfo deviceInfo : deviceInfos) {
			if (deviceInfo.getParentId() == null || deviceInfo.getParentId() == 0) {
				MenuInfo menuInfo = new MenuInfo(deviceInfo.getId(), deviceInfo.getDeviceName());
				list.add(menuInfo);
				iterator(deviceInfos, menuInfo);
			}
		}

		return R.ok(list);
	}

	private void iterator(List<ConfDeviceInfo> deviceInfos, MenuInfo menuInfo) {
		for (ConfDeviceInfo deviceInfo : deviceInfos) {
			if (deviceInfo.getParentId() != null && deviceInfo.getParentId() == menuInfo.getMenuId()){
				MenuInfo menuInfoItem = new MenuInfo(deviceInfo.getId(), deviceInfo.getDeviceName());
				if (menuInfo.getMenuInfos() == null) {
					menuInfo.setMenuInfos(new ArrayList<>());
				}
				List<MenuInfo> menuInfos = menuInfo.getMenuInfos();
				menuInfos.add(menuInfoItem);
				iterator(deviceInfos, menuInfoItem);
			}
		}
	}

}




